package com.hanhanxiaochong.zhongxiangchuanmei.presenter;

import com.hanhanxiaochong.zhongxiangchuanmei.config.HttpPostService;
import com.hanhanxiaochong.zhongxiangchuanmei.presenter.base.BasePresenter;
import com.hanhanxiaochong.zhongxiangchuanmei.view.iview.IIdCardView;
import com.trello.rxlifecycle.components.support.RxAppCompatActivity;
import com.wzgiceman.rxretrofitlibrary.retrofit_rx.Api.BaseApi;
import com.wzgiceman.rxretrofitlibrary.retrofit_rx.Api.BaseObserver;
import com.wzgiceman.rxretrofitlibrary.retrofit_rx.Api.ResponseBase;
import com.wzgiceman.rxretrofitlibrary.retrofit_rx.http.RetrofitSource;

import java.io.File;
import java.util.List;

import okhttp3.MediaType;
import okhttp3.MultipartBody;
import okhttp3.RequestBody;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

/**
 * Created by RockPhoenix on 2018/6/6.
 */

public class IdCardPresenter extends BasePresenter<IIdCardView> {

    public IdCardPresenter(IIdCardView iView) {
        super(iView);
    }

    @Override
    public void startPost(RxAppCompatActivity rxAppCompatActivity, BaseApi baseApi) {

    }

    @Override
    public void startPost(RxAppCompatActivity rxAppCompatActivity, BaseApi baseApi, int state) {

    }

    public void uploadPic(String token, String path) {
        iView.showLoadingDialog();
        File file = new File(path);
        MultipartBody.Builder builder = new MultipartBody.Builder()
                .setType(MultipartBody.FORM);//表单类型
        if (token != null)
            builder.addFormDataPart("token", token);
        RequestBody photoRequestBody = RequestBody.create(MediaType.parse("image/jpg"), file);
        builder.addFormDataPart("file[]", file.getName(), photoRequestBody);
        List<MultipartBody.Part> parts = builder.build().parts();
        addSubscription(RetrofitSource.createApi(HttpPostService.class).upload_multi(parts)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new BaseObserver<ResponseBase<List<String>>>(iView) {
                    @Override
                    public void onSuccess(ResponseBase<List<String>> response) {
                        iView.uploadSuccess(response.getData().get(0));
                    }
                }));
    }

    public void authentic(String token, String name, String idCardNum, String frontUrl, String backUrl) {
        iView.showLoadingDialog();
        addSubscription(RetrofitSource.createApi(HttpPostService.class)
                .auth(token, name, idCardNum, frontUrl, backUrl)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new BaseObserver<ResponseBase<String>>(iView) {
                    @Override
                    public void onSuccess(ResponseBase<String> response) {
                        iView.authenticSuccess(response.getData());
                    }
                }));
    }
}
